home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 371_01 / exampl3.c < prev    next >
Text File  |  1992-01-10  |  5KB  |  176 lines

  1. /***************************************************************************
  2. * EXAMPL3.C
  3. * This example demonstrates coroutines. They allow a sequential process
  4. * to use windows when prompting users.
  5. * In this case, the 'living trie' presents the user with the letters
  6. * of the alphabet. It then returns to Windows. When a letter is selected, it is
  7. * concatenated to the word being built up. Again the user is presented with
  8. * the letters of the alphabet and again control returns to Windows. When
  9. * the OK button is pressed, a letter is deleted from the end of the
  10. * word, and control returns back to the application.
  11. * Notice the absence of the run loop.
  12. *****************************************************************************/
  13.  
  14. #include <Windows.h>
  15. #include <WinDosIO.h>
  16.  
  17. #define UP_LEVEL     1
  18. #define SAME_LEVEL     2
  19. #define IDD_LISTBOX    100
  20. #define IDD_BUTTON    101
  21.  
  22. long far pascal WndProc(HWND, WORD, WORD, LONG);
  23. void LivingTrie(void);
  24.  
  25. HANDLE hInst;
  26. HWND hwnd, hwndStatic, hwndListBox, hwndButton, dummyHwnd;
  27.  
  28. char word[256];
  29. short level = 0;
  30.  
  31. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  32.            LPSTR lpszCmdline, int cmdshow)
  33.  {
  34.     WNDCLASS wc;
  35.     short i;
  36.     MSG msg;
  37.     char lBFill[2];
  38.  
  39.     lBFill[1] = 0;
  40.  
  41.     /* Register Main Window Class */
  42.  
  43.     hInst = hInstance;
  44.  
  45.     if (!hPrevInstance)
  46.      {
  47.         wc.style = 0;
  48.         wc.lpfnWndProc = WndProc;
  49.         wc.cbClsExtra = 0;
  50.         wc.cbWndExtra = 0;
  51.         wc.hInstance = hInstance;
  52.         wc.hIcon = LoadIcon( 0, IDI_APPLICATION );
  53.         wc.hCursor = LoadCursor( 0, IDC_ARROW );
  54.         wc.hbrBackground = COLOR_WINDOW + 1;
  55.         wc.lpszMenuName = "Example3";
  56.         wc.lpszClassName = "Example3";
  57.         RegisterClass( &wc );
  58.      }
  59.  
  60.  
  61.     /* Initialize terminal IO */
  62.     WinDosIO(WD_INIT,hInstance,0);
  63.  
  64.     /* Create Main Window */
  65.     hwnd = CreateWindow(
  66.                 "Example3",
  67.                 "The Living Trie",
  68.                 WS_OVERLAPPEDWINDOW,
  69.                 CW_USEDEFAULT,
  70.                 0,
  71.                 CW_USEDEFAULT,
  72.                 0,
  73.                 0,
  74.                 0,
  75.                 hInstance,
  76.                 NULL
  77.                 );
  78.  
  79.     ShowWindow(hwnd, cmdshow);
  80.  
  81.    /* Create a dummy termIO window, which will not even be used. */
  82.    dummyHwnd = CreateWindow("termIO",0,
  83.           WS_CHILD | WDS_NOPAGES | WDS_NOTEXTBUFFER,
  84.           0,0,0,0,hwnd,50,hInstance,0);
  85.    ShowWindow(dummyHwnd,SW_HIDE);
  86.  
  87.    hwndStatic = CreateWindow("static","",WS_CHILD | WS_VISIBLE | WS_BORDER,
  88.                      100,10,300,20,hwnd,0,hInst,0);
  89.  
  90.    hwndListBox = CreateWindow("listbox",0,WS_CHILD|WS_VISIBLE|LBS_NOTIFY |
  91.                        WS_VSCROLL | WS_BORDER,
  92.                       100,60,60,200,hwnd,IDD_LISTBOX,
  93.                       hInst,0);
  94.  
  95.    /* Fill the list box with the letters of the alphabet */
  96.    for (i = 'A'; i <= 'Z'; i++)
  97.     {
  98.       lBFill[0] = i;
  99.       SendMessage(hwndListBox,LB_ADDSTRING,0,(LONG)lBFill);
  100.     }
  101.  
  102.    hwndButton = CreateWindow("button","UP LEVEL",
  103.                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  104.                   100,280,100,40,hwnd,IDD_BUTTON,hInst,0);
  105.  
  106.    LivingTrie();
  107.  
  108.    WinDosIO(WD_DESTROY,hwnd,0);
  109.  
  110.    while( GetMessage( &msg, 0, 0, 0 ) != 0 )
  111.          {
  112.         TranslateMessage( &msg );
  113.         DispatchMessage( &msg );
  114.          }
  115.  
  116.  
  117.    return 0;
  118.  }
  119.  
  120.  
  121.  
  122. void LivingTrie(void)
  123.  {
  124.     short rc;
  125.     for(;;) {
  126.     word[level] = 0;
  127.     SendMessage(hwndStatic,WM_SETTEXT,0,(LONG)word);
  128.     if ((rc = ReturnToWindows()) == UP_LEVEL || rc < 0) break;
  129.      }
  130.  }
  131. /*************************************************************
  132. * 1. If user clicks on letter. Set word[level++] to letter.
  133. *    call Living trie. Then decrement level and ReturnToApplication(SAME_LEVEL)
  134. * 2. If user clicks on OK, call return to application(CLICKED_OK).
  135. **************************************************************/
  136.  
  137.  
  138. long far pascal WndProc(HWND hwnd, WORD msg, WORD wParam, LONG lParam)
  139.  {
  140.  
  141.     WORD notify = HIWORD(lParam);
  142.  
  143.     switch (msg) {
  144.         case WM_COMMAND:
  145.         switch (wParam)
  146.          {
  147.             case IDD_BUTTON:
  148.                  ReturnToApplication(UP_LEVEL);
  149.                  break;
  150.             case IDD_LISTBOX:
  151.                  if (notify == LBN_DBLCLK)
  152.                    {
  153.                      word[level++] = 'A' +
  154.                    SendMessage(hwndListBox,LB_GETCURSEL,0,0);
  155.                      LivingTrie();
  156.                      level--;
  157.                      ReturnToApplication(SAME_LEVEL);
  158.                           }
  159.                  break;
  160.             default:
  161.                 break;
  162.             } /* End switch wParam */
  163.     break;
  164.     case WM_SETFOCUS:
  165.         WinDosIO(WD_SETFOCUS,0,0);
  166.         break;
  167.     case WM_CLOSE:
  168.             WinDosIO(WD_DESTROY,hwnd,0);
  169.         return 0;
  170.         case WM_DESTROY:
  171.         PostQuitMessage(0);
  172.             return 0;
  173.     } /* End switch message */
  174.     return DefWindowProc(hwnd,msg,wParam,lParam);
  175. }
  176.